<?php

namespace App\Admin\Controllers;

use App\Models\Column;
use App\Models\Article;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class ArticleController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('文章管理');
            $content->description('列表');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('文章管理');
            $content->description('编辑');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('文章管理');
            $content->description('创建');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Article::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->title('标题');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Article::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->select('column_id','所属栏目')->options(Column::where('type',1)->pluck('title', 'id'))->placeholder('请输入所属栏目')->rules('required',[
                'required' => '请填写所属栏目',
            ]);

            $form->text('title','文章标题')->placeholder('请输入文章标题')->rules('required',[
                'required' => '请填写文章标题',
            ]);
            $form->text('copy_from','文章来源')->placeholder('例：腾讯新闻|http://new.qq.com/omn/20180410A01VOQ00')->rules('required',[
                'required' => '请填写文章来源',
            ]);
            $form->tags('keywords','关键词')->placeholder('请输入关键词')->rules('required',[
                'required' => '请填写关键词',
            ]);

            $form->editor('content', '文章内容');

            $form->textarea('description','文章描述')->rows(3);

            $form->select('state', '状态')->options([0 => '审核中', 1 => '已审核']);

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
